package com.lsp.rabbitmqcousumer.direct;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 正常队列消费者
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(
                        value = "zhengchang_queue",
                        /**备用死信队列**/
                        arguments = {
                                @Argument(name = "x-dead-letter-routing-key",value = "shixin_key"),
                                @Argument(name = "x-dead-letter-exchange",value = "shixin_exchange")
                        }
                ),
                exchange = @Exchange(
                        value = "zhengchang_exchange"),
                key =  "zhengchang_key")
)
public class NackOrDeadLetterReceiver1 {
    @RabbitHandler
    public void receiverAck(Map msg, Message message, Channel channel)  throws Exception{
        System.out.println("----------zhengchang-queue收到消息：" + msg + "当前时间：" + LocalDateTime.now());
        try {
//            int a = 1/0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            System.out.println("receiver success");
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
//            System.out.println("zhengchang-queue receiver Reject");
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            System.out.println("zhengchang-queue receiver fail");
        }
    }
}
